package cn.b.sky.user.controller;

import cn.b.sky.common.controller.BaseController;
import cn.b.sky.common.model.ImeCommon;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * 
 * <strong>Title : SecurityController.java<br>
 * </strong> <strong>Package : cn.b.sky.user.controller<br>
 * </strong> <strong>Description : </strong>继承imeController实现权限相关的数据接口<br>
 * <strong>Create on : 2016-4-26 下午2:34:13<br>
 * </strong>
 * <p>
 * 
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人|修改日期|修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
public class SecurityController extends BaseController {

  /**
   * 操作模式（增、改、查看）
   */
  protected static final String PATTERN = "pattern";

  @Override
  public String getCurrentLanguage() {

    return SecurityContextUtil.getLanguageCode();
  }

  /**
   * 
   * <strong>Title : setCreateInfo<br>
   * </strong> <strong>Description : </strong>设置创建修改信息<br>
   * <strong>Create on : 2016-4-26 下午2:29:13<br>
   * </strong>
   * <p>
   * 
   * @param imeCommon
   *            void
   * @throws
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  public void setCreateInfo(ImeCommon imeCommon) {
    imeCommon.setCreateDateTime(new Date());
    imeCommon.setCreateUser(SecurityContextUtil.getCurrentUserName());
    imeCommon.setSiteCode(this.getSiteCode());
    this.setModifyInfo(imeCommon);
  }

  public void setModifyInfo(ImeCommon imeCommon) {
    imeCommon.setModifyDateTime(new Date());
    imeCommon.setModifyUser(SecurityContextUtil.getCurrentUserName());
  }

  @Override
  public String getSiteCode() {
    return SecurityContextUtil.getSiteCode();
  }

  @Override
  public String getImeViewMessageI18n(String lang, String code, String... parameters) {
    if (StringUtils.isBlank(lang)) {
      lang = this.getCurrentLanguage();
    }
    if (code.indexOf(",") > 0) {
      String message = "";
      for (String c : code.split(",")) {
        if (!StringUtils.isBlank(c)) {
          message += LocaleResourceBundle.getCommonResourceLabel(getSiteCode(), lang, c);
        }
      }
      return message;
    } else {
      return LocaleResourceBundle.getCommonResourceLabel(getSiteCode(), lang, code, parameters);
    }
  }
}
